package gq.example.utils;

import org.jose4j.json.JsonUtil;
import org.jose4j.jwa.AlgorithmConstraints;
import org.jose4j.jwk.RsaJsonWebKey;
import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException;
import org.springframework.stereotype.Component;

import java.security.PrivateKey;
import java.security.PublicKey;

@Component
public class JwtUtil {

    private static String PUBLIC_KEY = "{\"kty\":\"RSA\",\"n\":\"2ZeW-u1zDvzhdPG2h-U23DQlIO2hgs7cbuopvQrPdWKAIQK196xo8vS-cZmk9KAQ4spkguvMuxWZ3FSqVK_P0suk3cQ8sGhUJJGUxZ7oZ7re9jDdqqhwajIFRLPzNk-TYpJs7RHpS3F0ZbuSNf9e1vi3sA5O4OXIgC5jHobS66qCKXBiDTNmivdrTwWrdHPP1uezG5OCC31icSF8lLMvLPD9RInBjCr3ZtW8zIHwHYxyWdu49CX4cYPML4r8YiWkaQWfcwLc0r6UDBdGvuHblKTHMgFRK14VNazSH3nCYBAXmFhFNbhllFJ62IfarVVELpu_-_bdYCGN2Omvth8CKQ\",\"e\":\"AQAB\"}";



    private static String PRIVATE_KEY = "{\"kty\":\"RSA\",\"n\":\"2ZeW-u1zDvzhdPG2h-U23DQlIO2hgs7cbuopvQrPdWKAIQK196xo8vS-cZmk9KAQ4spkguvMuxWZ3FSqVK_P0suk3cQ8sGhUJJGUxZ7oZ7re9jDdqqhwajIFRLPzNk-TYpJs7RHpS3F0ZbuSNf9e1vi3sA5O4OXIgC5jHobS66qCKXBiDTNmivdrTwWrdHPP1uezG5OCC31icSF8lLMvLPD9RInBjCr3ZtW8zIHwHYxyWdu49CX4cYPML4r8YiWkaQWfcwLc0r6UDBdGvuHblKTHMgFRK14VNazSH3nCYBAXmFhFNbhllFJ62IfarVVELpu_-_bd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V7CFDKYkFI2psyK_RryirW1uydthc4EaElLZ0Qkls\",\"qi\":\"T5BudOBWjxWKO_tstugaGFowBSTGbs5xJtgiAVU8nf-YpvzIokv9qWEHXhTlqbN5yuxR7PPjy542sECfJxDM0qEkddCU_bCwjLg6znJKxY7yWd8HlC0QQnBhO9UysyKgDe8URhn8mUk7KcyL6QwsIXLGzjaC1JRHf3d6Or_tF6A\"}";

    public static String sign(int uid, String username) throws JoseException {
        // 创建jwtclains jwt内容的载荷部分
        JwtClaims claims = new JwtClaims();
        // 失效时长
        claims.setExpirationTimeMinutesInTheFuture(30000000);

        claims.setClaim("uid", uid);
        claims.setClaim("username", username);

        // 签名
        JsonWebSignature jws = new JsonWebSignature();
        // 赋值载荷
        jws.setPayload(claims.toJson());

        // jwt使用私钥签署
        PrivateKey privateKey = new RsaJsonWebKey(JsonUtil.parseJson(PRIVATE_KEY)).getPrivateKey();

        jws.setKey(privateKey);

        // 设置签名算法
        jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_PSS_USING_SHA256);

        // 生成jwt
        String jwt = jws.getCompactSerialization();

        return jwt;
    }

    public static JwtClaims parseToken(String token) throws InvalidJwtException, JoseException {

        PublicKey publicKey = new RsaJsonWebKey(JsonUtil.parseJson(PUBLIC_KEY)).getRsaPublicKey();

        JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                .setVerificationKey(publicKey)
                .setRequireExpirationTime() // 设置过期时间
                .setJwsAlgorithmConstraints(new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_PSS_USING_SHA256))
                .build();


        return jwtConsumer.processToClaims(token);

    }
}
